package com.typy.www.tpay.ui.acitivity;

import android.content.ClipboardManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.bean.HomeBtnBean;
import com.typy.www.tpay.bean.listvm.BannerListVm;
import com.typy.www.tpay.bean.listvm.IcoTypeListVm;
import com.typy.www.tpay.bean.vm.BannerVm;
import com.typy.www.tpay.bean.vm.IcoTypeVm;
import com.typy.www.tpay.bean.vm.MyAccontVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.ui.adapter.IcotypeAdapter;
import com.typy.www.tpay.ui.adapter.MineGridAdapter;
import com.typy.www.tpay.ui.prt.PtrLinearContent;
import com.typy.www.tpay.ui.prt.PtrListContent;
import com.typy.www.tpay.util.BannerUtil;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.GsonUtils;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/7/9.
 */

public class DigitalAssetsActivity extends BaseBackActivity implements OnItemClickListener {

    GridView gvFour;
    MineGridAdapter adapter;
    @BindView(R.id.ll_listhold)
    LinearLayout llListhold;
    private ViewHolder holder;
    private PtrLinearContent ptrLinearContent;
    private PtrListContent ptrListContent;
    private ListView mListVm;
    private IcotypeAdapter icotypeAdapter;
    private List<IcoTypeVm> iconTypedata = new ArrayList<>();
    private List<BannerVm> bannerlist = new ArrayList<>();
    List<HomeBtnBean> mHomeList = new ArrayList<>();

    public static void show(Context context) {
        IntentUtil.intent2Activity(context, DigitalAssetsActivity.class);
    }

    @Override
    protected int getTitleBarResId() {
        return -1;
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_digitalassets;
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void refresh() {
        getBannerlistData();
        getMyaccountData();
        getIcotypelistData(1, true);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        addListView();
        initAdapter();
        getBannerlistData();
        getMyaccountData();
        getIcotypelistData(1, true);
        holder.convenientBanner.setOnItemClickListener(this);
    }

    private void addListView() {
        ptrListContent = new PtrListContent(getActivity()) {
            @Override
            protected void getPage(int pageIndex, boolean refresh) {
                if (refresh)
                    refresh();
                else
                    getIcotypelistData(pageIndex, refresh);
            }
        };
        mListVm = ptrListContent.getListView();
        llListhold.addView(ptrListContent.getmView());
    }

    private void initAdapter() {
        View headView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_gihi_head1, null);
        holder=new ViewHolder(headView);
        setListener();
        View topView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_grid, null);
        gvFour = topView.findViewById(R.id.gv_four);
        adapter = new MineGridAdapter(getActivity());
        adapter.setmType(1);
        gvFour.setAdapter(adapter);
        gvFour.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Object obj = adapterView.getItemAtPosition(i);
                if (obj instanceof HomeBtnBean) {
                    switch (((HomeBtnBean) obj).getBtnName()) {
                        case "转出":
                            ActivicationActivity.show(getActivity(), ClassHoldUtil.TurnOutDigi);
                            break;
                        case "众筹":
                            IcoActivity.show(getActivity());
                            break;
                        case "Z宝":
                            ZBaoActivity.show(getActivity());
                            break;
                        case "交易":
                            if (iconTypedata != null && iconTypedata.get(0) != null)
                                DigitalAssetsDetailActivity.show(getActivity(), iconTypedata.get(0).getFdName(), iconTypedata.get(0).getFdSeqNo()
                                        , iconTypedata.get(0).getFdStartPrice(), iconTypedata.get(0).getFdEndPrice(), iconTypedata.get(0).getFdPrice() + "", iconTypedata.get(0).getFdId());
                            break;
                    }
                }
            }
        });
        mListVm.addHeaderView(headView);
        mListVm.addHeaderView(topView);
        icotypeAdapter = new IcotypeAdapter(getActivity());
        icotypeAdapter.setmType(ClassHoldUtil.DigitalAssets);
        mListVm.setAdapter(icotypeAdapter);
        setList();
    }

    private void setList() {
        if (mHomeList != null && mHomeList.size() == 0) {
            mHomeList.add(HomeBtnBean.getBean("转出", R.drawable.shuzizichan_zhuanchu));
            mHomeList.add(HomeBtnBean.getBean("众筹", R.drawable.shuzizichan_ico));
            mHomeList.add(HomeBtnBean.getBean("Z宝", R.drawable.shuzizichan_zbao));
            mHomeList.add(HomeBtnBean.getBean("交易", R.drawable.shuzizichan_jiaoyi));
        }
        adapter.setList(mHomeList);
        adapter.notifyDataSetChanged();
    }
    private void setListener(){
        holder.btnTitlebarLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        holder.tvCopyAdrress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                cm.setText(holder.tvPayAddress.getText());
                Toast.makeText(getActivity(), "复制成功", Toast.LENGTH_LONG).show();
            }
        });
    }


    private void getBannerlistData() {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdTypeId", 1);
        HTTPUtils.post(getActivity(), Constance.getBannerlistURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                BannerListVm listVm = GsonUtils.parseJSON(ret.getData().toString(), BannerListVm.class);
                if (listVm != null && listVm.getData() != null) {
                    bannerlist = listVm.getData();
                    BannerUtil.showBanner(holder.convenientBanner, bannerlist);
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {

            }
        });
    }

    private void getMyaccountData() {
        ParamsUtil paramsUtil = new ParamsUtil();
        HTTPUtils.post(getActivity(), Constance.getMyaccountURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                MyAccontVm vm = GsonUtils.parseJSON(ret.getData().toString(), MyAccontVm.class);
                if (vm != null) {
                    updateMycount(vm);
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
            }
        });
    }

    private void getIcotypelistData(int pageIndex, final boolean refresh) {
        ParamsUtil params = new ParamsUtil();
        params.put("pageNum", pageIndex);
        params.put("pageSize", Constance.LIMT);
        HTTPUtils.post(getActivity(), Constance.getIcotypelistURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (refresh)
                    iconTypedata.clear();
                IcoTypeListVm vm = GsonUtils.parseJSON(ret.getData().toString(), IcoTypeListVm.class);
                if (vm != null && vm.getData() != null) {
                    iconTypedata.addAll(vm.getData());
                    icotypeAdapter.setData(iconTypedata);
                    icotypeAdapter.notifyDataSetChanged();
                }
                ptrListContent.loadInitialPage(mListVm.getCount());
            }

            @Override
            public void onResponseErr(int code, String msg) {
                ptrListContent.loadInitialPage(0);

            }
        });
    }

    private void updateMycount(MyAccontVm vm) {
        if (vm != null) {
            UpdataUiUtil.getDefaul().setText(vm.getFdPayAddress(), holder.tvPayAddress)
                    .setText(vm.getFdIconCount(), holder.tvZichan).setText(vm.getFdPrice(), holder.tvDangqianjiage);
        }
    }

    @Override
    public void onItemClick(int position) {
    }




//    @OnClick({R.id.btn_titlebar_left, R.id.tv_copyAdrress})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.btn_titlebar_left:
//                finish();
//                break;
//            case R.id.tv_copyAdrress:
//                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//                // 将文本内容放到系统剪贴板里。
//                cm.setText(holder.tvPayAddress.getText());
//                Toast.makeText(this, "复制成功", Toast.LENGTH_LONG).show();
//                break;
//        }
//    }

    static class ViewHolder {
        @BindView(R.id.convenientBanner)
        ConvenientBanner convenientBanner;
        @BindView(R.id.btn_titlebar_left)
        ImageView btnTitlebarLeft;
        @BindView(R.id.tv_PayAddress)
        TextView tvPayAddress;
        @BindView(R.id.tv_copyAdrress)
        TextView tvCopyAdrress;
        @BindView(R.id.ll_gonggao)
        PercentLinearLayout llGonggao;
        @BindView(R.id.tv_zichan)
        TextView tvZichan;
        @BindView(R.id.ll_amount)
        PercentLinearLayout llAmount;
        @BindView(R.id.tv_dangqianjiage)
        TextView tvDangqianjiage;
        @BindView(R.id.ll_point)
        PercentLinearLayout llPoint;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
